package com.rabbitmq.controller;

import com.rabbitmq.service.RabbitService;
import com.yx.tool.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

/**
 * @author yinxing
 * @date 2022-08-30
 */
@RestController
@Slf4j
public class SendController {

    @Value("${shop.exchange}")
    private String exchange;

    @Value("${shop.queue}")
    private String queue;

    @Value("${shop.routing-key}")
    private String routing_key;

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private RabbitService rabbitService;

    @GetMapping("/send")
    public Response send(String exchange_name, String route_key, String message) throws IOException {
        return rabbitService.sendMessage(exchange_name, route_key, message);
    }

    @GetMapping("/sendMsg/{message}")
    public void sendMsg(@PathVariable("message") String message) {
        log.info("============={}=发送消息:{}===============", new Date(System.currentTimeMillis()), message);
        rabbitTemplate.convertAndSend(exchange, routing_key, message);
    }


}
